package com.burgeon.backend.EnterpriseWeChat.rest.EWEmployee;

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.SHOPBACKEND.entity.vip.Employee;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 根据openId查询开卡人或服务导购
 */
public class findEmployeeByOpenId extends RESTBase {
    private String ip;
    private String account;
    private String password;

    public findEmployeeByOpenId(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     */

    public Employee getData(String openId) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(openId), url);
        System.err.println(str);

        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)){
            throw new Exception();
        }
        int count = new JSONArray(str).getJSONObject(0).getInt("count");
//        if (count==0) {
//            throw new Exception("无数据");
//        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
       // System.err.println(jsonArray);
        if(jsonArray.toString()!=null&& ! "[]".equals(jsonArray.toString())){
            Employee employee = new Employee();
            employee.setId(jsonArray.getJSONArray(0).getString(0));
            employee.setNO(jsonArray.getJSONArray(0).getString(1));
            employee.setName(jsonArray.getJSONArray(0).getString(2));
            employee.setPhone(jsonArray.getJSONArray(0).getString(3));
            employee.setcStoreId(jsonArray.getJSONArray(0).getString(4));
            return employee;
        }
        return null;

    }
    private MultiValueMap Data(String openId) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段
        columns.add("HR_EMPLOYEE_ID1");//ID
        columns.add("'HR_EMPLOYEE_ID1;NO'");//编号
        columns.add("'HR_EMPLOYEE_ID1;NAME'");//(AK)姓名PHONE
        columns.add("'HR_EMPLOYEE_ID1;PHONE'");
        columns.add("'HR_EMPLOYEE_ID1;C_STORE_ID'");//店仓Id



        params.put("columns", columns);
        params.put("table", "C_VIP");//表名

        params.put("count", "true");

        HashMap paramss = new HashMap();
        String ac =  "'="+openId+"'";//编号

        paramss.put("column", "OPENID2");
        paramss.put("condition", ac);
        params.put("params", paramss);

        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {//厚德"39.104.73.78:5321", "nea@burgeon.com.cn", "burgeon5321"
        //mgg"39.99.239.133:55322", "cawd@mgg.com", "123"
        findEmployeeByOpenId g = new findEmployeeByOpenId("39.104.73.78:5321", "nea@burgeon.com.cn", "burgeon5321");
        try {
            System.err.println("导购=="+g.getData("oFteI5DqOwdu9DSejvmq0TTmYUs0"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
